package qsoft.omanga.base.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import java.util.ArrayList;
import java.util.List;

import qsoft.omanga.business.entities.Manga;
import qsoft.omanga.common.utils.DataConfig;

/**
 * 
 * @author sontt
 * 
 */
public class MangaDAO {

	/**
     * 
     */
	private DBHelper dbHelper = null;

	/**
	 * @param context
	 *            Context
	 */
	public MangaDAO(Context context) {
		if (dbHelper == null) {
			dbHelper = new DBHelper(context);
		}
	}

	/**
	 * @param manga
	 *            Manga
	 * @return long
	 */
	public long insert(Manga manga) {
		long result = -1;
		dbHelper.open();
		ContentValues contentValues = new ContentValues();
		contentValues.put(DataConfig.MANGAID, manga.getId());
		contentValues.put(DataConfig.MANGA_NAME, manga.getName());
		contentValues.put(DataConfig.MANGA_AUTHOR, manga.getCreator1());
		contentValues.put(DataConfig.MANGA_DESCRIPT, manga.getIntro());
		result = dbHelper.insert(DataConfig.TABLE_MANGA, contentValues);
		dbHelper.close();
		return result;
	}

	/**
	 * @param order
	 *            String
	 * @return ArrayList<Manga>
	 */
	public List<Manga> getAllManga(String order) {

		List<Manga> list = new ArrayList<Manga>();
		dbHelper.open();
		Cursor cursor = dbHelper.getAll(DataConfig.TABLE_MANGA,
				new String[] { DataConfig.ALL }, order);
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				Manga manga = new Manga();
				manga.setId(cursor.getString(cursor
						.getColumnIndex(DataConfig.MANGAID)));
				manga.setName(cursor.getString(cursor
						.getColumnIndex(DataConfig.MANGA_NAME)));
				manga.setIntro(cursor.getString(cursor
						.getColumnIndex(DataConfig.MANGA_DESCRIPT)));
				list.add(manga);
				cursor.moveToNext();
			}
		}
		if (cursor != null) {
			cursor.close();
		}
		dbHelper.close();
		return list;
	}

	/**
	 * @param mangaId
	 *            String
	 * @return Manga
	 */
	public Manga getManga(String mangaId) {
		Manga manga = new Manga();
		String where = DataConfig.MANGAID + "= '" + mangaId + "'";
		dbHelper.open();
		Cursor cursor = dbHelper.getCondition(DataConfig.MANGAID,
				new String[] {}, where, DataConfig.MANGAID);
		if (cursor != null && cursor.getCount() > 0) {
			cursor.moveToFirst();
			manga.setId(cursor.getString(0));
			manga.setName(cursor.getString(1));
			manga.setCreator1(cursor.getString(2));
			manga.setIntro(cursor.getString(3));
		}
		dbHelper.close();
		return manga;
	}

	/**
	 * @param mangaId
	 *            String
	 * @return boolean
	 */
	public boolean existManga(String mangaId) {
		String where = DataConfig.MANGAID + " = " + mangaId;
		dbHelper.open();
		Cursor cursor = dbHelper.getCondition(DataConfig.TABLE_MANGA,
				new String[] { "*" }, where, DataConfig.MANGAID);
		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			cursor.close();
			return true;
		}
		dbHelper.close();
		return false;
	}
}
